﻿
using Irony.Compiler;

namespace IronLua.Ast {

	public class ForStatement : Statement {

		readonly VariableExpression variable;
		readonly Expression initializer;
		readonly Expression limit;
		readonly Expression step;
		readonly BlockStatement body;

		public VariableExpression Variable {
			get { return variable; }
		}

		public Expression Initializer {
			get { return initializer; }
		}

		public Expression Limit {
			get { return limit; }
		}

		public Expression Step {
			get { return step; }
		}

		public BlockStatement Body {
			get { return body; }
		}

		public override LuaNodeType NodeType {
			get { return LuaNodeType.ForStatement; }
		}

		public ForStatement (CompilerContext context, NonTerminal nonTerminal, SourceLocation location, AstNodeList childNodes)
			: base (context, nonTerminal, location, childNodes)
		{
			variable = childNodes [1].ToExpression ().To<VariableExpression> ();
			initializer = childNodes [3].ToExpression ();
			limit = childNodes [5].ToExpression ();
			step = childNodes [6] != null ? childNodes [6].ChildNodes [0].ToExpression () : null;
			body = childNodes [8].To<BlockStatement> ();
		}

		public override void Accept (ILuaAstVisitor visitor)
		{
			visitor.VisitForStatement (this);
		}
	}
}
